﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class OwnerTransferBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly ILotAttributeDA iLotattributeDA = DataAccess.CreateLotAttributeDA();
        private static readonly IInventoryMgtDA inventoryMgtDA = DataAccess.CreateInventoryMgtDA();
        private static readonly IInvLotDA invlotDA = DataAccess.CreateInvLotDA();
        private static readonly IInvLotLocLpnDA invLotLocLpnDA = DataAccess.CreateInvLotLocLpnDA();
        private static readonly IInvSkuLocDA invskuloc = DataAccess.CreateInvSkuLocDA();
        private static readonly ITransferPickDetailDA itransferPickDetailDA = DataAccess.CreateTransferPickDetailDA();
        private static readonly ITransferPickDetailDA iTransferPickDetailDA = DataAccess.CreateTransferPickDetailDA();
        private static readonly IOwnerDA ownerDA = DataAccess.CreateOwnerDA();
        private PubParasInfo pubParasInfo;
        private static readonly IRotationRuleDA rotataionRuleDA = DataAccess.CreateRotationRuleDA();
        private static readonly IRotationRuleDetailDA rotataionRuleDetailDA = DataAccess.CreateRotationRuleDetailDA();
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();
        private static readonly IOwnerTransferDA transferDA = DataAccess.CreateOwnerTransferDA();
        private static readonly IOwnerTransferDetailDA transferDetailDA = DataAccess.CreateOwnerTransferDetailDA();

        public OwnerTransferBLL()
        {
            this.pubParasInfo = null;
        }

        public OwnerTransferBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public OwnerTransferBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string AutoRevertAllocate(string transferID, string updatedBy)
        {
            string temp = string.Empty;
            string whLoginID = this.dataBaseID[0];
            string strStatusWhere = string.Empty;
            strStatusWhere = "  AND  (STATUS ='" + OwnerTransferStatus.Allocated + "' OR STATUS ='" + OwnerTransferStatus.PartAllocated + "' )";
            List<OwnerTransferDetailInfo> ownerTransferDetailInfo = transferDetailDA.GetOwnerTransferDetailListByStatus(transferID, strStatusWhere, this.dataBaseID[0]);
            int num = 0;
            if ((ownerTransferDetailInfo != null) && (ownerTransferDetailInfo.Count > 0))
            {
                foreach (OwnerTransferDetailInfo info in ownerTransferDetailInfo)
                {
                    if ((info.Status == OwnerTransferStatus.Allocated) || (info.Status == OwnerTransferStatus.PartAllocated))
                    {
                        List<TransferPickDetailInfo> transferPickDetailList = itransferPickDetailDA.GetTransferPickDetailByOrderID(info.TransferID, info.LineID.ToString(), whLoginID);
                        DataBase dataBase = new DataBase();
                        DbConnection conn = dataBase.connection;
                        conn.Open();
                        DbTransaction tran = conn.BeginTransaction();
                        try
                        {
                            foreach (TransferPickDetailInfo Pickinfo in transferPickDetailList)
                            {
                                InvLotInfo invLotInfo = new InvLotInfo {
                                    Lot = Pickinfo.Lot,
                                    PickedQty = Pickinfo.Qty * -1M,
                                    UpdatedBy = updatedBy
                                };
                                invlotDA.UpdateInvLot(dataBase, tran, invLotInfo, InvLotUpdateType.PickedQtyOnly, whLoginID);
                                InvLotLocLpnInfo invLotLocLpnInfo = new InvLotLocLpnInfo {
                                    Lot = Pickinfo.Lot,
                                    Loc = Pickinfo.Loc,
                                    LpnID = Pickinfo.LpnID,
                                    PickedQty = Pickinfo.Qty * -1M,
                                    UpdatedBy = updatedBy
                                };
                                invLotLocLpnDA.UpdateInvLotLocLpn(dataBase, tran, invLotLocLpnInfo, InvLotLocLpnUpdateType.PickedQtyOnly, whLoginID);
                                InvSkuLocInfo invskulocInfo = new InvSkuLocInfo {
                                    Loc = Pickinfo.Loc,
                                    SkuID = Pickinfo.SkuID,
                                    OwnerID = Pickinfo.OwnerID,
                                    UpdatedBy = updatedBy,
                                    PickedQty = Pickinfo.Qty * -1M
                                };
                                invskuloc.UpdateInvSkuLoc(dataBase, tran, invskulocInfo, InvSkuLocUpdateType.PickedQtyOnly, whLoginID);
                            }
                            itransferPickDetailDA.DeleteTransferPickDetail(dataBase, tran, info.TransferID, info.LineID.ToString(), whLoginID);
                            transferDetailDA.ChangeOwnerTransferDetailStatus(dataBase, tran, info.TransferID, info.LineID.ToString(), OwnerTransferStatus.New, updatedBy, whLoginID);
                            num++;
                            temp = temp + info.LineID + ",";
                            tran.Commit();
                        }
                        catch (Exception ex)
                        {
                            tran.Rollback();
                            CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                        }
                        finally
                        {
                            conn.Close();
                        }
                        strStatusWhere = "  AND STATUS ='" + OwnerTransferStatus.Confirmed + "'  ";
                        if (transferDetailDA.GetOwnerTransferDetailListByStatus(transferID, strStatusWhere, this.dataBaseID[0]).Count > 0)
                        {
                            transferDA.ChangeOwnerTransferStatus(transferID, OwnerTransferStatus.PartConfirmed, updatedBy, whLoginID);
                        }
                        else if (num != ownerTransferDetailInfo.Count)
                        {
                            transferDA.ChangeOwnerTransferStatus(transferID, OwnerTransferStatus.PartAllocated, updatedBy, whLoginID);
                        }
                        else
                        {
                            transferDA.ChangeOwnerTransferStatus(transferID, OwnerTransferStatus.New, updatedBy, whLoginID);
                        }
                    }
                }
            }
            return temp;
        }

        public string DeleteOwnerTransferInfo(List<string> transferID)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                List<OwnerTransferInfo> ownerTransferInfoList = new List<OwnerTransferInfo>();
                OwnerTransferInfo OwnerTransferInfo = null;
                foreach (string id in transferID)
                {
                    OwnerTransferInfo = transferDA.GetOwnerTransferByID(dataBase, tran, id, this.dataBaseID[0]);
                    ownerTransferInfoList.Add(OwnerTransferInfo);
                    iTransferPickDetailDA.DeleteTransferPickDetail(dataBase, tran, id, this.dataBaseID[0]);
                    transferDetailDA.DeleteOwnerTransferDetail(dataBase, tran, id, this.dataBaseID[0]);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (transferDA.DeleteOwnerTransfer(dataBase, tran, transferID, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < ownerTransferInfoList.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<OwnerTransferInfo>(dataBase, tran, ownerTransferInfoList[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "CONST_OWNER_TRANSFER", this.dataBaseID[0]);
                        temp = temp + transferID[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public string ExecuteInvOwnerTransfer(string transferID, string updatedBy)
        {
            string temp = string.Empty;
            string whLoginID = this.dataBaseID[0];
            string strStatusWhere = string.Empty;
            strStatusWhere = "  AND  (STATUS ='" + OwnerTransferStatus.Allocated + "' OR STATUS ='" + OwnerTransferStatus.PartConfirmed + "' )";
            List<OwnerTransferDetailInfo> ownerTransferDetailInfo = transferDetailDA.GetOwnerTransferDetailListByStatus(transferID, strStatusWhere, whLoginID);
            InventoryMgtBLL inventoryMgtBLL = new InventoryMgtBLL(this.pubParasInfo);
            if ((ownerTransferDetailInfo == null) || (ownerTransferDetailInfo.Count <= 0))
            {
                throw new Exception("未找到需要确认的数据!");
            }
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                foreach (OwnerTransferDetailInfo info in ownerTransferDetailInfo)
                {
                    List<TransferPickDetailInfo> tempTransferPickDetailInfos = itransferPickDetailDA.GetTransferPickDetailByOrderID(info.TransferID, info.LineID.ToString(), whLoginID);
                    for (int i = 0; i < tempTransferPickDetailInfos.Count; i++)
                    {
                        LotAttributeInfo lotAttributeInfo = iLotattributeDA.GetLotAttributeByID(tempTransferPickDetailInfos[i].Lot, whLoginID);
                        ReceiptLotInfo receiptLotInfo = new ReceiptLotInfo {
                            LotAttr01 = lotAttributeInfo.LotAttr01,
                            LotAttr02 = lotAttributeInfo.LotAttr02,
                            LotAttr03 = lotAttributeInfo.LotAttr03,
                            LotAttr04 = lotAttributeInfo.LotAttr04,
                            LotAttr05 = lotAttributeInfo.LotAttr05,
                            LotAttr06 = lotAttributeInfo.LotAttr06,
                            LotAttr07 = lotAttributeInfo.LotAttr07,
                            LotAttr08 = lotAttributeInfo.LotAttr08,
                            LotAttr09 = lotAttributeInfo.LotAttr09,
                            LotAttr10 = lotAttributeInfo.LotAttr10,
                            ExpiryDate = lotAttributeInfo.ExpiryDate,
                            ExternalLot = lotAttributeInfo.ExternalLot,
                            ProduceDate = lotAttributeInfo.ProduceDate,
                            ReceiptDate = lotAttributeInfo.ReceiptDate,
                            VendorID = lotAttributeInfo.VendorID
                        };
                        string strtoLot = new InboundHelper().CheckLotAttributeInfo(receiptLotInfo, info.ToOwnerID, info.ToSkuID, whLoginID, this.pubParasInfo.UserID);
                        InvMovesInfo invMovesInfo = new InvMovesInfo(info.FromOwnerID, info.FromSkuID, tempTransferPickDetailInfos[i].Lot, tempTransferPickDetailInfos[i].Loc, tempTransferPickDetailInfos[i].LpnID, info.ToOwnerID, info.ToSkuID, strtoLot, tempTransferPickDetailInfos[i].Loc, tempTransferPickDetailInfos[i].LpnID, tempTransferPickDetailInfos[i].Qty, tempTransferPickDetailInfos[i].PackID, tempTransferPickDetailInfos[i].UomID, tempTransferPickDetailInfos[i].Qty, tempTransferPickDetailInfos[i].PackID, tempTransferPickDetailInfos[i].UomID, updatedBy, info.TransferID, info.LineID);
                        inventoryMgtBLL.UpdateInvLotLocLpn4Moves(dataBase, tran, invMovesInfo, InvMoveType.OwnerTransfer, whLoginID);
                        inventoryMgtBLL.UpdateInvSkuLoc4Moves(dataBase, tran, invMovesInfo, InvMoveType.OwnerTransfer, whLoginID);
                        inventoryMgtBLL.UpdateLpn4Moves(dataBase, tran, invMovesInfo, whLoginID);
                        inventoryMgtBLL.UpdateInvLot4Moves(dataBase, tran, invMovesInfo, InvMoveType.OwnerTransfer, whLoginID);
                        inventoryMgtBLL.InsertInvMovesTrans(dataBase, tran, invMovesInfo, TransSourceType.TransferDetailAdd, whLoginID);
                        InvMovesInfo invFromMovesInfo = new InvMovesInfo("", "", "", tempTransferPickDetailInfos[i].Loc, tempTransferPickDetailInfos[i].LpnID, info.FromOwnerID, info.FromSkuID, tempTransferPickDetailInfos[i].Lot, tempTransferPickDetailInfos[i].Loc, tempTransferPickDetailInfos[i].LpnID, tempTransferPickDetailInfos[i].Qty * -1M, tempTransferPickDetailInfos[i].PackID, tempTransferPickDetailInfos[i].UomID, tempTransferPickDetailInfos[i].Qty, tempTransferPickDetailInfos[i].PackID, tempTransferPickDetailInfos[i].UomID, updatedBy, info.TransferID, info.LineID);
                        inventoryMgtBLL.InsertInvMovesTrans(dataBase, tran, invFromMovesInfo, TransSourceType.TransferDetailUpdate, whLoginID);
                        transferDetailDA.ChangeOwnerTransferDetailStatus(dataBase, tran, info.TransferID, info.LineID.ToString(), TransferStatus.Confirmed, updatedBy, whLoginID);
                        temp = temp + tempTransferPickDetailInfos[i].OrderLineID.ToString() + ",";
                    }
                }
                strStatusWhere = "  AND  (STATUS ='" + OwnerTransferStatus.New + "' OR STATUS ='" + OwnerTransferStatus.Allocated + "' )";
                if (transferDetailDA.GetOwnerTransferDetailListByStatus(dataBase, tran, transferID, strStatusWhere, whLoginID).Count > 0)
                {
                    transferDA.ChangeOwnerTransferStatus(dataBase, tran, transferID, OwnerTransferStatus.PartConfirmed, updatedBy, this.dataBaseID[0]);
                }
                else
                {
                    transferDA.ChangeOwnerTransferStatus(dataBase, tran, transferID, OwnerTransferStatus.Confirmed, updatedBy, this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp.Substring(0, temp.Length - 1);
        }

        public DataSet GetInvOwnerTransferInfoByQueryEntity(OwnerTransferQueryEntity transferQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(transferQuery.TransferID))
            {
                sqlWhere.Add(" TRANSFER_ID " + transferQuery.TransferID);
            }
            if (!string.IsNullOrEmpty(transferQuery.ExternalTransferID))
            {
                sqlWhere.Add(" EXTERNAL_TRANSFER_ID " + transferQuery.ExternalTransferID);
            }
            if (!string.IsNullOrEmpty(transferQuery.Status))
            {
                sqlWhere.Add(" STATUS " + transferQuery.Status);
            }
            if (!string.IsNullOrEmpty(transferQuery.TransferType))
            {
                sqlWhere.Add(" TRANSFER_TYPE " + transferQuery.TransferType);
            }
            return transferDA.GetOwnerTransferByQueryList(sqlWhere, transferQuery, this.dataBaseID[0]);
        }

        public OwnerTransferInfo GetOwnerTransferInfoByID(string transferID)
        {
            OwnerTransferInfo transfer = transferDA.GetOwnerTransferByID(transferID, this.dataBaseID[0]);
            if (transfer != null)
            {
                transfer.OwnerTransferDetailInfos = transferDetailDA.GetOwnerTransferDetailListByID(transferID, this.dataBaseID[0]);
                transfer.TransferPickDetailInfos = itransferPickDetailDA.GetTransferPickDetailByOrderID(transferID, this.dataBaseID[0]);
            }
            return transfer;
        }

        private string GetOwnerTransferInfoStatus(List<OwnerTransferDetailInfo> transferDetailInfos)
        {
            int totalCount = 0;
            for (int i = 0; i < transferDetailInfos.Count; i++)
            {
                if (transferDetailInfos[i].Status == OwnerTransferStatus.Confirmed)
                {
                    totalCount++;
                }
            }
            if (totalCount == transferDetailInfos.Count)
            {
                return OwnerTransferStatus.Confirmed;
            }
            return OwnerTransferStatus.New;
        }

        public string InsertOwnerTransferInfo(OwnerTransferInfo transferInfo)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string transferID = string.Empty;
            try
            {
                if (!ownerDA.CheckTradePartnerIDUnique(transferInfo.FromOwnerID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F5300_001", this.pubParasInfo.UserLanguage, new List<string> { transferInfo.FromOwnerID });
                }
                else if (!commonMethod.CheckAuthorizationOwnerByUserID(this.pubParasInfo.UserID, transferInfo.FromOwnerID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F5300_002", this.pubParasInfo.UserLanguage, new List<string> { transferInfo.FromOwnerID });
                }
                if (!ownerDA.CheckTradePartnerIDUnique(transferInfo.ToOwnerID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F5300_003", this.pubParasInfo.UserLanguage, new List<string> { transferInfo.ToOwnerID });
                }
                else if (!commonMethod.CheckAuthorizationOwnerByUserID(this.pubParasInfo.UserID, transferInfo.ToOwnerID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F5300_004", this.pubParasInfo.UserLanguage, new List<string> { transferInfo.ToOwnerID });
                }
                transferInfo.TransferID = commonMethod.GenNextNumStr(dataBase, tran, "TRANSFER", this.dataBaseID[0]);
                transferInfo.UpdatedBy = transferInfo.CreatedBy;
                transferInfo.CreatedDate = DateTime.Now;
                transferInfo.UpdatedDate = DateTime.Now;
                if (transferDA.InsertOwnerTransfer(dataBase, tran, transferInfo, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<OwnerTransferInfo>(dataBase, tran, null, transferInfo, AuditAction.Insert, this.pubParasInfo.UserID, "TRANSFER", this.dataBaseID[0]);
                    transferID = transferInfo.TransferID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return transferID;
        }

        public string TransferAllocate(string transferID, string updatedBy)
        {
            string strTemp = string.Empty;
            string strStatusWhere = string.Empty;
            strStatusWhere = "  AND  (STATUS ='" + OwnerTransferStatus.New + "' OR STATUS =' " + OwnerTransferStatus.PartAllocated + "')";
            List<OwnerTransferDetailInfo> ownerTransferDetailInfp = transferDetailDA.GetOwnerTransferDetailListByStatus(transferID, strStatusWhere, this.dataBaseID[0]);
            int num = 0;
            if ((ownerTransferDetailInfp != null) && (ownerTransferDetailInfp.Count > 0))
            {
                Dictionary<string, SkuInfo> skuObjects = new Dictionary<string, SkuInfo>();
                Dictionary<string, RotationRuleInfo> rotationRuleObjects = new Dictionary<string, RotationRuleInfo>();
                Dictionary<string, PackInfo> packInfoObjects = new Dictionary<string, PackInfo>();
                Dictionary<string, InvLotInfo> invLotObjects = new Dictionary<string, InvLotInfo>();
                OutboundMgtBLL outboundMgtBLL = new OutboundMgtBLL(this.pubParasInfo);
                decimal qty = 0M;
                foreach (OwnerTransferDetailInfo info in ownerTransferDetailInfp)
                {
                    DataBase dataBase = new DataBase();
                    DbConnection conn = dataBase.connection;
                    conn.Open();
                    DbTransaction tran = conn.BeginTransaction();
                    try
                    {
                        SkuInfo skuInfo = null;
                        if (!skuObjects.ContainsKey(info.FromOwnerID + info.FromSkuID))
                        {
                            skuInfo = skuDA.GetSkuByID(dataBase, tran, info.FromOwnerID, info.FromSkuID, this.dataBaseID[0]);
                            skuObjects.Add(info.FromOwnerID + info.FromSkuID, skuInfo);
                        }
                        else
                        {
                            skuObjects.TryGetValue(info.FromOwnerID + info.FromSkuID, out skuInfo);
                        }
                        if (skuInfo != null)
                        {
                            RotationRuleInfo rotationRuleInfo = null;
                            if (!rotationRuleObjects.ContainsKey(skuInfo.RotationRuleID))
                            {
                                rotationRuleInfo = rotataionRuleDA.GetRotationRuleByID(dataBase, tran, skuInfo.RotationRuleID, this.dataBaseID[0]);
                                if (rotationRuleInfo == null)
                                {
                                    throw new Exception("货品" + info.FromSkuID + "没有指定周转规则 分配失败！");
                                }
                                rotationRuleInfo.RotationRuleDetailInfo = rotataionRuleDetailDA.GetRotationRuleDetailListByID(dataBase, tran, rotationRuleInfo.RotationRuleID, this.dataBaseID[0]);
                                rotationRuleObjects.Add(skuInfo.RotationRuleID, rotationRuleInfo);
                            }
                            else
                            {
                                rotationRuleObjects.TryGetValue(skuInfo.RotationRuleID, out rotationRuleInfo);
                            }
                            string orderBy = outboundMgtBLL.OrganizeRotationRule(rotationRuleInfo);
                            LotAttributeInfo lotAttribute = new LotAttributeInfo {
                                OwnerID = info.FromOwnerID,
                                SkuID = info.FromSkuID,
                                LotAttr01 = info.TransferLotInfo.LotAttr01,
                                LotAttr02 = info.TransferLotInfo.LotAttr02,
                                LotAttr03 = info.TransferLotInfo.LotAttr03,
                                LotAttr04 = info.TransferLotInfo.LotAttr04,
                                LotAttr05 = info.TransferLotInfo.LotAttr05,
                                LotAttr06 = info.TransferLotInfo.LotAttr06,
                                LotAttr07 = info.TransferLotInfo.LotAttr07,
                                LotAttr08 = info.TransferLotInfo.LotAttr08,
                                LotAttr09 = info.TransferLotInfo.LotAttr09,
                                LotAttr10 = info.TransferLotInfo.LotAttr10,
                                ProduceDate = info.TransferLotInfo.ProduceDate,
                                ExpiryDate = info.TransferLotInfo.ExpiryDate,
                                ReceiptDate = info.TransferLotInfo.ReceiptDate,
                                ExternalLot = info.TransferLotInfo.ExternalLot,
                                VendorID = info.TransferLotInfo.VendorID,
                                Status = "OK"
                            };
                            List<InventoryInfo> inventoryInfos = inventoryMgtDA.GetInventoryInfoByLotAttribute(lotAttribute, info.FromLoc, info.FromLpnID, orderBy, this.dataBaseID[0]);
                            qty = info.ToQty;
                            if ((inventoryInfos != null) && (inventoryInfos.Count > 0))
                            {
                                decimal invQty = 0M;
                                foreach (InventoryInfo invInfo in inventoryInfos)
                                {
                                    if (invInfo.Qty > 0M)
                                    {
                                        invQty = (((((invInfo.Qty - invInfo.AllocatedQty) - invInfo.PickedQty) - invInfo.ArchiveQty) - invInfo.ExpectedQty) - invInfo.PickinprocessQty) - invInfo.PendingMoveIn;
                                        InvLotInfo invLotInfo = null;
                                        if (invLotObjects.ContainsKey(invInfo.Lot))
                                        {
                                            invLotObjects.TryGetValue(invInfo.Lot, out invLotInfo);
                                        }
                                        else
                                        {
                                            invLotInfo = invlotDA.GetInvLotByID(dataBase, tran, invInfo.Lot, this.dataBaseID[0]);
                                            invLotObjects.Add(invInfo.Lot, invLotInfo);
                                        }
                                        decimal lotAvailableQty = (((invLotInfo.Qty - invLotInfo.PreallocatedQty) - invLotInfo.AllocatedQty) - invLotInfo.PickedQty) - invLotInfo.OnholdQty;
                                        if (invQty > lotAvailableQty)
                                        {
                                            invQty = lotAvailableQty;
                                            invLotInfo.AllocatedQty += invQty;
                                        }
                                        else if (invQty <= lotAvailableQty)
                                        {
                                            invLotInfo.AllocatedQty += invQty;
                                        }
                                        invLotObjects[invInfo.Lot] = invLotInfo;
                                        if (invQty >= qty)
                                        {
                                            qty = 0M;
                                            invQty = info.ToQty;
                                        }
                                        else if (invQty > 0M)
                                        {
                                            info.ToQty -= invQty;
                                            qty = info.ToQty;
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                        TransferPickDetailInfo transferPickDetailInfo = new TransferPickDetailInfo();
                                        transferPickDetailInfo.PickDetailID = commonMethod.GenNextNumStr(dataBase, tran, "TRANSFER_PICK_DETAIL", this.dataBaseID[0]);
                                        transferPickDetailInfo.SkuID = invInfo.SkuID;
                                        transferPickDetailInfo.UomID = info.FromUomID;
                                        transferPickDetailInfo.FromLoc = invInfo.Loc;
                                        transferPickDetailInfo.Loc = invInfo.Loc;
                                        transferPickDetailInfo.Lot = invInfo.Lot;
                                        transferPickDetailInfo.LpnID = invInfo.LpnID;
                                        transferPickDetailInfo.OrderID = info.TransferID;
                                        transferPickDetailInfo.OrderLineID = info.LineID;
                                        transferPickDetailInfo.OwnerID = invInfo.OwnerID;
                                        transferPickDetailInfo.PackID = info.FromPackID;
                                        transferPickDetailInfo.ToLoc = invInfo.Loc;
                                        transferPickDetailInfo.Qty = invQty;
                                        transferPickDetailInfo.QtyRejected = 0M;
                                        transferPickDetailInfo.AltSkuID = "";
                                        transferPickDetailInfo.BatchCartonID = "";
                                        transferPickDetailInfo.CartonGroup = "";
                                        transferPickDetailInfo.CartonType = "";
                                        transferPickDetailInfo.CaseID = "";
                                        transferPickDetailInfo.CompanyPrefix = "";
                                        transferPickDetailInfo.CrossDocked = "";
                                        transferPickDetailInfo.DoCartonize = "";
                                        transferPickDetailInfo.Door = "";
                                        transferPickDetailInfo.DoReplenish = "";
                                        transferPickDetailInfo.DropID = "";
                                        transferPickDetailInfo.EffectiveDate = DateTime.Now;
                                        transferPickDetailInfo.FreightCharges = 0.0;
                                        transferPickDetailInfo.InterModalVehicle = "";
                                        transferPickDetailInfo.IsClosed = "";
                                        transferPickDetailInfo.LabelType = "";
                                        transferPickDetailInfo.LoadID = "";
                                        transferPickDetailInfo.MovedQty = 0M;
                                        transferPickDetailInfo.OptimizeCop = "";
                                        transferPickDetailInfo.PickMethod = "";
                                        transferPickDetailInfo.PickNotes = "";
                                        transferPickDetailInfo.PreallocatePickDetailID = "";
                                        transferPickDetailInfo.QcStatus = "";
                                        transferPickDetailInfo.ReceiptID = "";
                                        transferPickDetailInfo.RejectedReason = "";
                                        transferPickDetailInfo.ReplenishZone = "";
                                        transferPickDetailInfo.Route = "";
                                        transferPickDetailInfo.SeqNo = 0;
                                        transferPickDetailInfo.SerialReference = "";
                                        transferPickDetailInfo.SortationLocation = "";
                                        transferPickDetailInfo.SortationStation = "";
                                        transferPickDetailInfo.Status = "";
                                        transferPickDetailInfo.Stop = 0;
                                        transferPickDetailInfo.TrackingID = "";
                                        transferPickDetailInfo.UomQty = 0M;
                                        transferPickDetailInfo.UpdateSource = "";
                                        transferPickDetailInfo.WaveID = "";
                                        transferPickDetailInfo.FreightCharges = 0.0;
                                        transferPickDetailInfo.CreatedBy = this.pubParasInfo.UserID;
                                        transferPickDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
                                        itransferPickDetailDA.InsertTransferPickDetail(dataBase, tran, transferPickDetailInfo, this.dataBaseID[0]);
                                        InvLotInfo invLotinfo = new InvLotInfo {
                                            Lot = invInfo.Lot,
                                            UpdatedBy = updatedBy,
                                            PickedQty = invQty
                                        };
                                        invlotDA.UpdateInvLot(dataBase, tran, invLotinfo, InvLotUpdateType.PickedQtyOnly, this.dataBaseID[0]);
                                        InvLotLocLpnInfo invLotLocLpnInfos = new InvLotLocLpnInfo {
                                            Lot = invInfo.Lot,
                                            Loc = invInfo.Loc,
                                            LpnID = invInfo.LpnID,
                                            PickedQty = invQty,
                                            UpdatedBy = updatedBy
                                        };
                                        invLotLocLpnDA.UpdateInvLotLocLpn(dataBase, tran, invLotLocLpnInfos, InvLotLocLpnUpdateType.PickedQtyOnly, this.dataBaseID[0]);
                                        InvSkuLocInfo invskulocInfo = new InvSkuLocInfo {
                                            Loc = invInfo.Loc,
                                            SkuID = invInfo.SkuID,
                                            OwnerID = invInfo.OwnerID,
                                            UpdatedBy = updatedBy,
                                            PickedQty = invQty
                                        };
                                        invskuloc.UpdateInvSkuLoc(dataBase, tran, invskulocInfo, InvSkuLocUpdateType.PickedQtyOnly, this.dataBaseID[0]);
                                        transferDetailDA.ChangeOwnerTransferDetailStatus(dataBase, tran, info.TransferID, info.LineID.ToString(), OwnerTransferStatus.Allocated, updatedBy, this.dataBaseID[0]);
                                        if (qty == 0M)
                                        {
                                            break;
                                        }
                                    }
                                }
                                if (qty == 0M)
                                {
                                    num++;
                                    tran.Commit();
                                    strTemp = strTemp + info.LineID + "分配成功!;";
                                }
                                else if (qty > 0M)
                                {
                                    tran.Rollback();
                                    strTemp = strTemp + info.LineID + "分配失败,库存数量不足,无法继续分配!;";
                                }
                            }
                            else
                            {
                                tran.Rollback();
                                strTemp = strTemp + info.LineID + "分配失败,无法找到满足的库存进行分配;";
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            if (num == ownerTransferDetailInfp.Count)
            {
                transferDA.ChangeOwnerTransferStatus(transferID, OwnerTransferStatus.Allocated, updatedBy, this.dataBaseID[0]);
                return strTemp;
            }
            if (num > 0)
            {
                transferDA.ChangeOwnerTransferStatus(transferID, OwnerTransferStatus.PartAllocated, updatedBy, this.dataBaseID[0]);
            }
            return strTemp;
        }
    }
}

